<template>
  <el-container>
    <!--  头部  -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏菜单     -->
      <el-aside :width="isWidth + 'px'">
        <div class="toggle-button" @click="toggleHandle">|||</div>
        <el-menu :default-active="activePath" router :collapse-transition="false" :collapse="isClose" background-color="#313743" text-color="#fff" active-text-color="#3b9cff" unique-opened>
          <!--   一级菜单       -->
          <el-submenu :index="item.id + ''" :key="item.id" v-for="(item) in menuList">
            <!--    一级菜单模板区域        -->
            <template slot="title">
              <!--  图标  -->
              <i :class="iconsObj[item.id]" style="margin-right: 5px"></i>
              <!--  文本  -->
              <span>{{item.authName}}</span>
            </template>
              <!-- 二级菜单 -->
              <el-menu-item @click="activeSessionPage('/'+result.path)" :index="'/'+result.path" :key="result.id" v-for="result in item.children">
                <template slot="title">
                  <!--  图标  -->
                  <i class="el-icon-menu"></i>
                  <!--  文本  -->
                  <span>{{result.authName}}</span>
                </template>
              </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menuList: [],
      //
      iconsObj: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 菜单折叠
      isClose: false,
      isWidth: 200,
      activePath: ''
    }
  },
  created() {
    this.getCreateMenu()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有菜单
    async getCreateMenu() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return console.log('获取菜单错误!')
      this.menuList = res.data
    },
    // 隐藏菜单
    toggleHandle() {
      this.isClose = !this.isClose
      if (this.isClose) {
        this.isWidth = 64
      } else {
        this.isWidth = 200
      }
    },
    activeSessionPage(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style scoped lang="less">
.el-header {
  background-color: #363d40;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: white;
  font-size: 20px;
  div {
    display: flex;
    align-items: center;
    span {
      padding-left: 15px;
    }
  }
}
.el-aside {
  background-color: #313743;
  .el-menu {
    border-right: 0;
  }
}
.el-main {
  background-color: #e9edf1;
}
section {
  height: 100%;
}
.toggle-button {
  background-color: #475164;
  text-align: center;
  line-height: 20.8px;
  font-size: 10px;
  color: white;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
